package models

import (
	"gorm.io/gorm"
)

type Goods struct {
	gorm.Model
	ImageId     int64
	TypesId     int64
	Title       string
	Price       float64
	Stock       int64
	GoodsStatus int64 `gorm:"type:int;default:1;comment:默认1 为上架 2为下架"`
	StatusId    int64
}

func (g *Goods) GetGoods(page, pageSize int) (listGoods []*Goods, err error) {
	err = DB.
		Scopes(Paginate(page, pageSize)).
		Find(&listGoods).Error
	return
}

func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {

		if page <= 0 {
			page = 1
		}

		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}

func (g *Goods) GetGoodsById(goodsId int64) error {
	return DB.Where("id=?", goodsId).Limit(1).Find(&g).Error
}

func (g *Goods) CreateGoods() error {
	return DB.Create(&g).Error
}

type Images struct {
	gorm.Model
	GoodsId int64  `gorm:"type:int(3);comment:商品ID"`
	Url     string `gorm:"type:varchar(255);comment:图片地址"`
}
type Types struct {
	gorm.Model
	Name string `gorm:"type:varchar(20);comment:分类名称"`
}
type AuditLog struct {
	gorm.Model
	GoodsId     int64
	AuditStatus int64 `gorm:"type:int;default:1;comment:默认1 为审核通过 2为驳回"`
}
type Visit struct {
	gorm.Model
	GoodsId int64 //商品ID
	Visits  int64 //浏览量
}

type GoodsVisitInfo struct {
	TypeId  int64  //分类ID
	Name    string //分类名称
	GoodsId int64  //商品ID
	Title   string //商品名称
	Visits  int64  //浏览次数
}
